package com.zhangyu.shadow.repair.delegate.history;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.zhangyu.shadow.repair.R;
import com.zhangyu.shadow.repair.R2;
import com.zhangyu.shadow.repair.delegate.bean.CommonBean;
import com.zhangyu.shadow.repair.delegate.bean.VehicleBean;
import com.zhangyu.shadow.repair.delegate.history.detail.HistoryDetailDelegate;
import com.zhangyu.shadow.repair.manager.AccountManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;
import st.lib.base.delegate.mvp.MvpDelegate;
import st.lib.base.ui.datepicker.DateUtils;
import st.lib.base.ui.recycler.RecyclerViewNoBugLinearLayoutManager;
import st.lib.base.util.utilcode.util.ScreenUtils;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/20
 */
public class HistoryDelegate extends MvpDelegate<HistoryPresenter>
        implements HistoryContract.View {
    public static HistoryDelegate create() {
        return new HistoryDelegate();
    }

    private static final int PAGE_SIZE = 20;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_date_start)
    TextView mTvDateStart;
    @BindView(R2.id.tv_date_end)
    TextView mTvDateEnd;
    @BindView(R2.id.et_code)
    EditText mEtCode;
    @BindView(R2.id.recycler_view_maintain_type)
    RecyclerView mRecyclerViewMaintainType;
    @BindView(R2.id.recycler_view_classification)
    RecyclerView mRecyclerViewClassification;
    private List<String> mMaintainTypes = new ArrayList<>();
    private List<String> mClassifications = new ArrayList<>();
    private String mBusNumber = "";
    private int mBusVmType = 0;
    private String mDateStart = "";
    private String mDateEnd = "";
    private String mMinTime = "";
    private HistoryAdapter mHistoryAdapter;
    private boolean isRefresh;
    private MaintainTypeAdapter mTypeAdapter;

    @Override
    public Object setLayout() {
        if (ScreenUtils.isPad(_mActivity)) {
            return R.layout.delegate_history;
        } else {
            return R.layout.delegate_history_phone;
        }
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        mToolbar.setTitle("维保历史");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
        mRecyclerView.setLayoutManager(new RecyclerViewNoBugLinearLayoutManager(getContext()));

        mHistoryAdapter = new HistoryAdapter(getContext(),
                R.layout.item_bus_history_search, null);
        mHistoryAdapter.setEmptyView(R.layout.view_list_empty,
                (ViewGroup) mRecyclerView.getParent());
        mHistoryAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isRefresh = false;
                onLoadMore();
            }
        }, mRecyclerView);
        mRecyclerView.setAdapter(mHistoryAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {

            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (!isFastClick()) {
                    VehicleBean bean = (VehicleBean) adapter.getItem(position);
                    if (bean != null) {
                        start(HistoryDetailDelegate.create(String.valueOf(bean.getId()),
                                String.valueOf(bean.getVmType())));
                    }
                }
            }
        });
        mDateStart = DateUtils.getNowCalendar(-1);
        mDateEnd = DateUtils.getNowCalendar(0);
        mTvDateStart.setText(mDateStart);
        mTvDateEnd.setText(mDateEnd);
        mRecyclerViewMaintainType.setLayoutManager(new LinearLayoutManager(_mActivity,
                LinearLayoutManager.HORIZONTAL, false));
        mRecyclerViewClassification.setLayoutManager(new LinearLayoutManager(_mActivity,
                LinearLayoutManager.HORIZONTAL, false));

        mRecyclerViewMaintainType.setAdapter(mTypeAdapter = new MaintainTypeAdapter(R.layout.item_cb, mMaintainTypes,
                null));
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mPresenter.getMaintainType();
        mPresenter.getRepairType();
        mPresenter.getClassification();
        onSearch();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultHorizontalAnimator();
    }

    @OnClick(R2.id.btn_search)
    public void onSearch() {
        mHistoryAdapter.setEmptyView(R.layout.view_list_loading,
                (ViewGroup) mRecyclerView.getParent());
        mHistoryAdapter.setNewData(null);
        mBusNumber = mEtCode.getText().toString();
        mDateStart = mTvDateStart.getText().toString();
        mDateEnd = mTvDateEnd.getText().toString();
        mMinTime = "";
        isRefresh = true;
        onLoadMore();
    }

    private void onLoadMore() {
        mPresenter.getHistoryList(mMaintainTypes, mClassifications,
                AccountManager.getInstance().getDepId(),
                mBusNumber, mDateStart, mDateEnd, mBusVmType, mMinTime);
    }

    @Override
    public void showLoading(String str) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public HistoryPresenter getPresenter() {
        return new HistoryPresenter(this);
    }

    @Override
    public void showHistoryList(List<VehicleBean> list) {
        final int size = list == null ? 0 : list.size();
        if (isRefresh) {
            mHistoryAdapter.setEmptyView(R.layout.view_list_empty,
                    (ViewGroup) mRecyclerView.getParent());
            mHistoryAdapter.setNewData(list);
        } else {
            if (size > 0) {
                mHistoryAdapter.addData(list);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mHistoryAdapter.loadMoreEnd(isRefresh);
        } else {
            VehicleBean bean = list.get(size - 1);
            mMinTime = bean.getOrderTime();
            mHistoryAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showMaintainList(List<MaintainType> list) {
        mTypeAdapter.addData(list);
    }

    @Override
    public void showClassificationList(List<CommonBean> list) {
        mRecyclerViewClassification.setAdapter(new ClassificationAdapter(R.layout.item_cb,
                mClassifications,
                list));
    }

    @OnClick({R2.id.tv_date_start, R2.id.tv_date_end})
    public void onDateClick(View view) {
        final int id = view.getId();
        final Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(_mActivity, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String date = year + "-" +
                        ((month + 1) > 9 ? (month + 1) : "0" + (month + 1)) +
                        "-" + (dayOfMonth > 9 ? dayOfMonth : "0" + dayOfMonth);
                if (id == R.id.tv_date_start) {
                    mTvDateStart.setText(date);
                } else {
                    mTvDateEnd.setText(date);
                }
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    @Override
    public boolean onBackPressedSupport() {
        if (!isFastClick()) {
            pop();
        }
        return true;
    }

}
